<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>05_异步函数-await处理异步请求</title>
  <!-- 
  
  -->
</head>
<body>

  <script>
    function requestData(url) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve(url)
          // reject("error message")
        }, 2000)
      })
    }

    async function getData() {
      // 如果是异步请求，await会等待异步请求的结果
      // 返回reject，则会被catch捕获, 会输出err:  error message终止执行后面的代码
      const url = await requestData('http://www.baidu.com')
      console.log("res1: ", url)

      const data = await requestData(url + '/data')
      console.log("res2: ", data);
    }

    getData().catch(err => {
      console.log("err: ", err);
    })

  </script>
  
</body>
</html>